import java.awt.Color;
import java.util.ArrayList;

public class Graph {
	private ArrayList<Edge> m_edges = new ArrayList<Edge>();
	private ArrayList<Vertex> m_vertexes = new ArrayList<Vertex>();
	private int m_edgeCount = 0;
	private int m_vertexCount = 0;
	public static final int maxVertexCount = 13;

	public ArrayList<Edge> getEdges() {
		return m_edges;
	}

	public ArrayList<Vertex> getVertexes() {
		return m_vertexes;
	}

	public int getEdgeCount() {
		return m_edgeCount;
	}

	public int getVertexCount() {
		return m_vertexCount;
	}

	public void addVertex(String name) {
		m_vertexes.add(new Vertex(0, 0, name));
		m_vertexCount = m_vertexes.size();
	}

	public void addEdge(Vertex from, Vertex to, int weight) {
		Edge new_edge = new Edge(from, to, weight);
		m_edges.add(new_edge);
		from.addLink(new_edge);
		to.addLink(new_edge);
		m_edgeCount = m_edges.size();
	}

	public void deleteVertex(Vertex v) {
		m_vertexes.get(m_vertexes.indexOf(v)).free();
		m_vertexes.remove(v);
		m_vertexCount = m_vertexes.size();
	}

	public void deleteEdge(Edge e) {
		m_edges.get(m_edges.indexOf(e)).free();
		m_edges.remove(e);
		m_edgeCount = m_edges.size();
	}

	public void reset() {
		for(Edge eg: m_edges) {
			eg.setColor(Color.GRAY);
			eg.setIsPath(false);
		}
		for(Vertex vx: m_vertexes) {
			vx.setColor(Color.GRAY);
			vx.setPathLength(0);
			vx.setPathEdge(null);
		}
	}

	public Vertex getByName(String name) {
		for(Vertex v: m_vertexes) {
			if(v.getName().equals(name)) {
				return v;
			}
		}
		return null;
	}

	public Edge getByNameEdge(String name_from, String name_to) {
		for(Edge e: m_edges) {
			if ( (e.getFrom().getName().equals(name_from) & e.getTo().getName().equals(name_to)) |  
				 (e.getFrom().getName().equals(name_to)   & e.getTo().getName().equals(name_from))) {
				return e;
			}
		}
		return null;
	}

	public void generateGraph(int radius, int centreX, int centreY) {
		if (getVertexCount() != 0) {
			int angle = 360/getVertexCount();
			double fi = (angle * 3.14159265358979323846/180);
			int x, y;
			for (int i = 0; i < getVertexCount(); i++) {
				x = (int) (radius * Math.cos(fi * i) + centreX);
				if(Math.sin(fi * i) < 0) {
					y = (int) Math.abs(radius * Math.sin(fi * i)) + centreY + 1;
				}
				else {
					y = (int)( centreY - radius*Math.sin(fi * i)); 
				}
				m_vertexes.get(i).setCoords(x, y);
			}
			for (Edge e : m_edges) {
				e.refreshPos();
			}
		}
	}

}
